using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class pages_Register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Util util = new Util();
        cmbCountry.DataSource = util.GetCountryList();
        cmbCountry.DataBind();
    }
    protected void lnkRegister_Click(object sender, EventArgs e)
    {
        Validate();
        if (!IsValid) return;
        ccCaptcha.ValidateCaptcha(txtCaptcha.Text);
        if (ccCaptcha.UserValidated)
        {
            PersonDAO daoPerson = new PersonDAO();
            UserDAO daoUser = new UserDAO();
            Person per = new Person();
            User user = new User();
            per.FirstName = txtFirstname.Text;
            per.LastName = txtLastName.Text;
            per.DateOfBirth = DateTime.Parse(txtDateOfBirth.Text);
            per.Gender = radMale.Checked ? true : false;
            per.Address = txtAddress.Text;
            per.City = txtCity.Text;
            per.State = txtState.Text;
            per.Country = cmbCountry.SelectedItem.Text;
            per.Phone = txtPhone.Text;
            per.Email = txtEmail.Text;
            per.ModifiedDate = DateTime.Now;
            per.ModifiedBy = 10;

            user.UserName = txtUsername.Text;
            user.Password =CypherTextUtil.MD5Encrypt(txtPassword.Text);
            user.RegisteredDate = DateTime.Now;
            user.LastLogin = DateTime.Now;
            user.Role = 10;
            user.PersonInfor = per;
            if (daoPerson.Insert(per) > 0)
            {
                if (daoUser.Insert(user) > 0)
                {
                    pnlMessageBox.Visible = true;
                    pnlRegistrationForm.Visible = false;
                }
                else lblMessage.Text = "Sorry. Error has occured when trying to create your account. Try to enter other Username";
            }
            else lblMessage.Text = "Sorry. Error has occured when trying to create your account. Please try this later";
        }
        else { lblMessage.Text = "Invalid confirm characters"; }
        txtCaptcha.Text = "";
    }
}
